#include<scheme.h>
#include<scheme-private.h>
#include<dynload.h>
#include<windows.h>
/*
#define DLLIMPORT __declspec (dllexport)

DLLIMPORT void init_test (scheme* sc);
extern void  addFuncsTEST(scheme *sc) ;
extern void  init_test(scheme *sc) ;
//SCHEME_EXPORT pointer scm_load_ext(scheme *sc, pointer arglist);
 static pointer square(scheme *sc, pointer args) {
           double v = 0;
           if(args!=sc->NIL) {
               if(is_number(pair_car(args))) {
                    v=rvalue(pair_car(args));
                    return mk_real(sc,v*v);
               }
           }
           return sc->NIL;
          }
          static pointer show_args(scheme *sc, pointer args) {
           double v = 0;
           if(args!=sc->NIL) {
               return args;
           }
           return sc->NIL;
          }
          
void addFuncsTEST(scheme* sc){
   scheme_define (sc, sc->global_env,
                   mk_symbol (sc, "square"),
                   mk_foreign_func (sc, square)) ;
                scheme_define (sc, 
                sc->global_env, 
                mk_symbol(sc,"show_args"), 
                mk_foreign_func(sc, show_args)); 
                   return; 
}
void init_test(scheme* sc){
   addFuncsTEST(sc);
                   sc->interface->scheme_define( 
        sc, 
        sc->global_env, 
        sc->interface->mk_symbol(sc,"square"), 
        sc->interface->mk_foreign_func(sc, square)); 
                   return; 
}

*/
int main(){
	scheme sc;
	pointer port;
	FILE *fin;
  char *file_name="init.ts";
  int retcode;
  int isfile=1;
  scheme_init(&sc);
  //scheme_define(&sc,sc.global_env,mk_symbol(sc,"load-extension"),mk_foreign_func(&sc, scm_load_ext));
  scheme_set_input_port_file(&sc, stdin);
  scheme_set_output_port_file(&sc, stdout);
  if(access(file_name,0)!=0) {
    char *p=getenv("TSINIT");
    if(p!=0) {
      file_name=p;
    }
  }
    fin=fopen(file_name,"r");
    if(fin==0) {
      fprintf(stderr,"Could not open file %s\n",file_name);
    } else {
      scheme_load_file(&sc,fin);
			if(isfile) {
				fclose(fin);
			}
    }
  retcode=sc.retcode;
	port = scheme_get_defined_var(&sc,"server-port");
	if(port == sc.NIL){
		printf("null\n");
	}else{
		printf("server-port: %s\n",string_value(port));
	}
	return 0;
}
